package com.way.capture.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceCategory;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;

import com.way.capture.R;
import com.way.capture.activity.SettingsActivity;
import com.way.capture.utils.ScrollUtils;

import static com.way.capture.utils.Settings.LONG_SCREENSHOT_AUTO;
import static com.way.capture.utils.Settings.NIGHT_MODE_KEY;
import static com.way.capture.utils.Settings.VIDEO_AUDIO_KEY;
import static com.way.capture.utils.Settings.VIDEO_SIZE_KEY;

/**
 * Created by android on 16-2-4.
 */
public class SettingsFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {
    private static final String VERSION_KEY = "key_version";
    private static final String ABOUT_AUTHOR_KEY = "key_about_author";
    private Context mContext;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        addPreferencesFromResource(R.xml.settings);
        try {
            PackageManager packageManager = mContext.getPackageManager();
            String packageName = mContext.getPackageName();
            // Update the version number
            final PackageInfo packageInfo = packageManager.getPackageInfo(packageName, 0);
            findPreference(VERSION_KEY).setSummary(packageInfo.versionName);
        } catch (Exception e) {
            findPreference(VERSION_KEY).setSummary("?");
        }

        if (!ScrollUtils.isDeviceRoot()) {
            PreferenceCategory preferenceScreen = (PreferenceCategory) findPreference("key_advance_category");
            preferenceScreen.removePreference(findPreference(LONG_SCREENSHOT_AUTO));
        }
        PreferenceManager.getDefaultSharedPreferences(getActivity()).registerOnSharedPreferenceChangeListener(this);
    }

    @Override
    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
        String key = preference.getKey();
        switch (key) {
            case VIDEO_AUDIO_KEY:
                break;
            case VERSION_KEY:
                break;
            case ABOUT_AUTHOR_KEY:
                break;
            default:
                break;
        }
        return super.onPreferenceTreeClick(preferenceScreen, preference);
    }


    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        switch (key) {
            case VIDEO_AUDIO_KEY:
                break;
            case NIGHT_MODE_KEY:
                //reboot();
                break;
            case VIDEO_SIZE_KEY:

                break;
        }
    }

    public void reboot() {
        Intent intent = new Intent(mContext, SettingsActivity.class);
        startActivity(intent);
        ((Activity) mContext).finish();
        ((Activity) mContext).overridePendingTransition(0, android.R.anim.fade_out);
    }
}
